package entity;

/**
 * @Author: wanqian
 * @Date: 2021/2/8 17:03
 */
public class HuffCodeNode implements Comparable<HuffCodeNode> {

    /**
     * 字符
     */
    private Byte data;

    /**
     * 权值
     */
    private int weight;

    private HuffCodeNode left;

    private HuffCodeNode right;

    public HuffCodeNode(Byte data, int weight) {
        this.data = data;
        this.weight = weight;
    }

    public HuffCodeNode(HuffCodeNode leftNode, HuffCodeNode rightNode) {
        this.weight = leftNode.weight + rightNode.weight;
        this.left = leftNode;
        this.right = rightNode;
    }

    public int getWeight() {
        return weight;
    }

    public void setWeight(int weight) {
        this.weight = weight;
    }

    public HuffCodeNode getLeft() {
        return left;
    }

    public void setLeft(HuffCodeNode left) {
        this.left = left;
    }

    public HuffCodeNode getRight() {
        return right;
    }

    public void setRight(HuffCodeNode right) {
        this.right = right;
    }

    public Byte getData() {
        return data;
    }

    public void setData(Byte data) {
        this.data = data;
    }

    public void preOrder() {
        System.out.println(this);
        if (left != null) {
            left.preOrder();
        }
        if (right != null) {
            right.preOrder();
        }
    }

    @Override
    public int compareTo(HuffCodeNode o) {
        return this.weight - o.weight;
    }

    @Override
    public String toString() {
        return "HuffCodeNode{" +
                "data=" + data +
                ", weight=" + weight +
                '}';
    }
}
